{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from crypto import decrypt, encrypt, generate_keypair, save_key, load_key"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Создание пары ключей"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "keypair = generate_keypair(512)\n",
    "save_key(\"public.key\", keypair[0])\n",
    "save_key(\"private.key\", keypair[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Зашифрование"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "public_key = load_key(\"public.key\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "input.jpg: 1.28MB [00:07, 192kB/s]                            \n"
     ]
    }
   ],
   "source": [
    "encrypt(\"input.jpg\", public_key,  \"input.enc\", 64)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Расшифрование"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "private_key = load_key(\"private.key\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "input.enc: 1.28MB [01:56, 11.5kB/s]                            \n"
     ]
    }
   ],
   "source": [
    "decrypt(\"input.enc\", private_key, \"input_dec.jpg\", block_size=64)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Сравнение выходных данных"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original hash: 912274062dd0d931950bfa329d8b10425b4867c0ef78216a059285b913f6ee83\n",
      "Decrypted hash: 912274062dd0d931950bfa329d8b10425b4867c0ef78216a059285b913f6ee83\n"
     ]
    }
   ],
   "source": [
    "import hashlib\n",
    "\n",
    "def sha256sum(filename):\n",
    "    with open(filename, 'rb', buffering=0) as f:\n",
    "        return hashlib.file_digest(f, 'sha256').hexdigest()\n",
    "\n",
    "\n",
    "original_hash = sha256sum(\"input.jpg\")\n",
    "decrypted_hash = sha256sum(\"input_dec.jpg\")\n",
    "\n",
    "print(f\"Original hash: {original_hash}\")\n",
    "print(f\"Decrypted hash: {decrypted_hash}\")\n",
    "assert original_hash == decrypted_hash"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
